package se.combitech.combiorder.data;

import java.math.BigDecimal;

import javax.annotation.PostConstruct;
import javax.ejb.LocalBean;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import se.combitech.combiorder.model.Address;
import se.combitech.combiorder.model.Country;
import se.combitech.combiorder.model.Customer;
import se.combitech.combiorder.model.Product;

/**
 * Session Bean implementation class DataLoader
 */
@Singleton
@LocalBean
@Startup
public class DataLoader {

    @Inject
    EntityManager entityManager;

    /**
     * Default constructor.
     */
    public DataLoader() {

    }

    @PostConstruct
    public void load() {
        
        loadCountries();
        loadCustomers();
        loadProducts();

        entityManager.flush();
    }

    /**
     * 
     */
    private void loadProducts() {

        Product product = null;

        product = new Product();
        product.setTitle("Skottkärra");
        product.setDescription("Fin och robust skottkärra");
        product.setPrice(new BigDecimal(1495.00));
        entityManager.persist(product);

        product = new Product();
        product.setTitle("Spade");
        product.setDescription("Rostfri spade");
        product.setPrice(new BigDecimal(266.00));
        entityManager.persist(product);

        product = new Product();
        product.setTitle("Lövräfsa");
        product.setDescription("Hållbar lövräfsa");
        product.setPrice(new BigDecimal(295.11));
        entityManager.persist(product);

        product = new Product();
        product.setTitle("Vattenspridare");
        product.setDescription("Sprider vattnet fint");
        product.setPrice(new BigDecimal(577.00));
        entityManager.persist(product);

        entityManager.flush();
    }

    /**
     * 
     */
    private void loadCustomers() {

        Country country = getCountryByName("Sweden");

        entityManager.persist(newCustomer("Håkan", "Olsson", "070-194000",
                "hakan.olson@combitech.se", false).addAddress(
                new Address("Tabergsgatan 22", "58911", "Taberg", country)));

        Customer customer =
            newCustomer("Hans", "Johansson", "070-194001", "hans.johansson@combitech.se", true)
                    .addAddress(new Address("Smedbygatan 12", "56141", "Huskvarna", country))
                    .addAddress(new Address("Storgatan 5", "56139", "Huskvarna", country));

        entityManager.persist(customer);
        entityManager.persist(newCustomer("Torbjörn", "Lindén", "070-194002",
                "torbjorn.linden@combitech.se", false).addAddress(
                new Address("Skogen 33", "54542", "Bälaryd", country)));

        entityManager.flush();
    }

    private Country getCountryByName(String name) {

        Query query = entityManager.createQuery("select c from Country c where c.name = :name");
        query.setParameter("name", name);

        return (Country) query.getSingleResult();
    }

    /**
     * 
     */
    private void loadCountries() {
        entityManager.persist(new Country("Sweden"));
        entityManager.persist(new Country("Norway"));
        entityManager.persist(new Country("Denmark"));
        entityManager.persist(new Country("Finland"));
        entityManager.flush();

    }

    private Customer newCustomer(String firstName, String lastName, String phone, String email,
            boolean smsnotification) {

        Customer customer = new Customer();
        customer.setFirstName(firstName);
        customer.setLastName(lastName);
        customer.setPhone(phone);
        customer.setEmail(email);
        customer.setSmsnotification(smsnotification);

        return customer;
    }

}
